﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Agile.Engine.View
{
    public class ProxyViewProvider : ViewProvider
    {
        public ProxyViewProvider()
        {
            ServiceProcess = new Service.DefaultServiceProcess();

            IsEnableService = false;
        }

        protected Service.DefaultServiceProcess ServiceProcess { get; set; }

        public string ProxyDataContext { set { ServiceProcess.Context = value; } }
        public string ProxyRule { set { ServiceProcess.Rule = value; } }
        public Agile.Engine.Service.IClientServiceProxy Proxy
        {
            get { return ServiceProcess.Proxy; }
            set { ServiceProcess.Proxy = value; }
        }

        public virtual bool InvokeService(System.Collections.Hashtable args)
        {
            return ServiceProcess.InvokeService(args);
        }

        public bool IsEnableService { get; set; }

        public virtual bool UpdateLocal(System.Collections.Hashtable args)
        { return true; }

        public override bool ExecuteUpdate(System.Collections.Hashtable viewModel)
        {
            ///验证失败
            if (!ExecuteValidate(viewModel))
            {
                return false;
            }

            if (IsEnableService)
            {
                ///调用服务
                if (InvokeService(viewModel))
                {
                    return UpdateLocal(ServiceProcess.Result);
                }
                else
                {
                    return false;
                }
            }

            return UpdateLocal(viewModel);
        }
    }
}
